package com.cy.utils;

import jodd.props.Props;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: Dio
 * Date: 12-4-19
 * Time: 下午8:48
 * To change this template use File | Settings | File Templates.
 */
public class PropertyUtil {
    public static final String KERNEL = "kernel";
    public static final String UPLOAD = "upload";
    public static final String QINIU = "qiniu";
    public static final String WEIXIN = "weixin";
    public static final String WIFI = "wifi";
    public static final String EMAIL = "email";
    public static final String DB = "db";
    public static final String SOLR = "solr";
    public static final String DEVICE = "device";
    private static final String SEPROP = "clmp.properties";
    public static Map<String, Props> cacheMap = new HashMap<String, Props>();

    /**
     * 获取.properties值工具类
     *
     * @param propertiesFile
     * @param key
     * @param cache
     * @return
     */
    public static String getValue(String propertiesFile, String key, boolean cache) {
        String value = "";
        Props props = cacheMap.get(propertiesFile);
        if (props == null) {
            try {
                String fullFile;
                props = new Props();
                InputStream in = PropertyUtil.class.getResourceAsStream("/"+propertiesFile);
                props.load(in);
                if (cache) {
                    cacheMap.put(propertiesFile, props);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        value = props.getValue(key);
        return value;
    }

    /**
     * 获取.properties值工具类,默认缓存
     *
     * @param propertiesFile
     * @param key
     * @return
     */
    public static String getValue(String propertiesFile, String key) {
        return getValue(propertiesFile, key, true);
    }

    /**
     * 获取Grosse的系统配置
     *
     * @param g   组别
     * @param key 键
     * @return
     */
    public static String getGrosseValue(String g, String key) {
        return getGrosseValue(g + "." + key);
    }

    public static String getGrosseValue(String key) {
        return getValue(SEPROP, key);
    }

    private static void getStrings() {
        String str = "135960523930431-84995124";
        Pattern p = Pattern.compile("^[1]{1}[\\d]{10}");
        Matcher m = p.matcher(str);
        ArrayList<String> strs = new ArrayList<String>();
        while (m.find()) {
            strs.add(m.group(1));
        }
        for (String s : strs){
            System.out.println(s);
        }
    }

}